<template>
  <div class="app-container">
    <el-card style="border-radius: 10px">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="计划编号" prop="planCode">
          <el-input
            v-model="queryParams.planCode"
            placeholder="请输入计划编号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="计划名称" prop="planName">
          <el-input
            v-model="queryParams.planName"
            placeholder="请输入计划名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="来源类型" prop="orderSource">
          <el-select v-model="queryParams.orderSource" placeholder="请选择来源类型" clearable>
            <el-option
              v-for="dict in dict.type.mes_workorder_sourcetype"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="产品编号" prop="productCode">
          <el-input
            v-model="queryParams.productCode"
            placeholder="请输入产品编号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="产品名称" prop="productName">
          <el-input
            v-model="queryParams.productName"
            placeholder="请输入产品名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="单据状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择单据状态" clearable>
            <el-option
              v-for="dict in dict.type.pro_planning_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <div style="height: 15px"></div>
    <el-card style="border-radius: 10px">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['pro:planning:add']"
          >新增
          </el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table border v-loading="loading" :data="planningList" @selection-change="handleSelectionChange">
        <el-table-column label="计划编号" align="center" prop="planCode" width="150">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="handleInfoView(scope.row)">{{ scope.row.planCode }}</el-button>
          </template>
        </el-table-column>
        <el-table-column label="计划名称" align="center" prop="planName" width="150" :show-overflow-tooltip="true"/>
        <el-table-column label="来源类型" align="center" prop="orderSource" width="100">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.mes_workorder_sourcetype" :value="scope.row.orderSource"/>
          </template>
        </el-table-column>
        <el-table-column label="销售编号" align="center" prop="sourceCode" width="200" :show-overflow-tooltip="true"/>
        <el-table-column label="产品编号" align="center" prop="productCode" width="150"/>
        <el-table-column label="产品名称" align="center" prop="productName" width="150">
          <template slot-scope="scope">
            <el-tag>{{ scope.row.productName }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="规格型号" align="center" prop="productSpc" width="150" :show-overflow-tooltip="true"/>
        <el-table-column label="单位" align="center" prop="unitOfMeasure"/>
        <el-table-column label="计划开工时间" align="center" prop="startDate" width="120">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="计划完工时间" align="center" prop="endDate" width="120">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="需求日期" align="center" prop="requestDate" width="120">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.requestDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="计划数量" align="center" prop="quantity">
          <template slot-scope="scope">
            <el-tag type="danger">{{ scope.row.quantity }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="状态" align="left" prop="status">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.pro_planning_status" :value="scope.row.status"/>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="250px" align="left">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['pro:planning:edit']"
              v-if="scope.row.status == 'PREPARE'"
            >
              修改
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-video-play"
              @click="handleFenJie(scope.row)"
              v-hasPermi="['pro:planning:fenjie']"
              v-if="scope.row.status == 'DAIFENJIE'">
              执行工单
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-circle-check"
              @click="handleInfoSave(scope.row)"
              v-hasPermi="['pro:planning:save']"
              v-if="scope.row.status == 'PREPARE'">
              确认计划
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['pro:planning:remove']"
              v-if="scope.row.status == 'PREPARE'"
            >删除
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-remove"
              @click="handleDied(scope.row)"
              v-hasPermi="['pro:planning:query']"
              v-if="scope.row.status !== 'DIED' && scope.row.status !== 'PREPARE' && scope.row.status !== 'FINISHED'"
            >中止
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-refresh"
              @click="handleRestart(scope.row)"
              v-hasPermi="['pro:planning:query']"
              v-if="scope.row.status === 'DIED' "
            >开启
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <!-- 添加或修改生产计划对话框 -->
    <el-dialog :title="title" :visible.sync="open" v-if="open == true" width="960px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" :disabled="optType == 'view' || optType == 'save' || optType == 'update'">
        <el-row>
          <el-col :span="12">
            <el-form-item label="计划编号" prop="planCode">
              <el-input v-model="form.planCode" placeholder="请输入计划编号" :readonly="optType !='add'"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计划名称" prop="planName">
              <el-input v-model="form.planName" placeholder="请输入计划名称"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="来源类型" prop="orderSource">
              <el-select v-model="form.orderSource" placeholder="请选择来源类型" clearable>
                <el-option
                  v-for="dict in dict.type.mes_workorder_sourcetype"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="订单编号" prop="sourceCode">
              <el-input v-model="form.sourceCode" placeholder="请输入订单编号" label-width="100px">
                <el-button
                  slot="append"
                  @click="handleSelectSource"
                  v-if="optType != 'view' && optType != 'save'"
                  icon="el-icon-search"
                ></el-button>
              </el-input>
              <SourceSelect ref="sourceSelect" @onSelected="onSourceSelected"></SourceSelect>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="设备编号" prop="deviceCode">
              <el-input v-model="form.deviceCode" readonly/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12" v-if="form.orderSource == 'ORDER' && form.sourceCode">
            <el-form-item label="产品编号" prop="productCode">
              <el-input v-model="form.productCode" placeholder="请选择产品">
                <el-button
                  slot="append"
                  @click="handleSelectOrderProduct"
                  v-if="optType != 'view' && optType != 'save'"
                  icon="el-icon-search"
                ></el-button>
              </el-input>
              <ProductSelect
                :sourceCode="form.sourceCode"
                ref="productSelect"
                @onSelected="onProductSelected"
              ></ProductSelect>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-else>
            <el-form-item label="产品编号" prop="productCode">
              <el-input v-model="form.productCode" placeholder="请选择产品">
                <el-button
                  slot="append"
                  @click="handleSelectProduct"
                  v-if="optType != 'view' && optType != 'save'"
                  icon="el-icon-search"
                ></el-button>
              </el-input>
              <ItemSelect ref="itemSelect" @onSelected="onItemSelected"></ItemSelect>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品名称" prop="productName">
              <el-input v-model="form.productName" readonly placeholder="请选择产品"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="规格型号" prop="productSpc">
              <el-input v-model="form.productSpc" readonly placeholder="请选择产品"/>
            </el-form-item>
          </el-col>
          <!--          <el-col :span="12">-->
          <!--            <el-form-item label="物料类型">-->
          <!--              <el-radio-group v-model="form.itemOrProduct" disabled>-->
          <!--                <el-radio v-for="dict in dict.type.mes_item_product" :key="dict.value" :label="dict.value"-->
          <!--                >{{ dict.label }}-->
          <!--                </el-radio>-->
          <!--              </el-radio-group>-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->
          <el-col :span="12">
            <el-form-item label="单位" prop="unitOfMeasure">
              <el-input v-model="form.unitOfMeasure" readonly placeholder="请选择产品"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="开工时间" prop="startDate">
              <el-date-picker
                clearable
                v-model="form.startDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择开工时间"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="完工时间" prop="endDate">
              <el-date-picker
                clearable
                v-model="form.endDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择完工时间"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="需求日期" prop="requestDate">
              <el-date-picker
                clearable
                v-model="form.requestDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择需求日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计划数量" prop="quantity">
              <el-input-number :min="1" v-model="form.quantity" placeholder="请输入计划数量"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="客户名称" prop="clientName">
              <el-input readonly v-model="form.clientName" placeholder="请输入客户名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="合并相同产品" prop="mergeSameProduct">
              <el-radio-group v-model="form.mergeSameProduct">
                <el-radio v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.value">{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-tabs type="border-card" v-if="form.planId != null">
        <el-tab-pane label="产品制程">
          <PlanBom ref="bomlist" :planning="form" :optType="optType"></PlanBom>
        </el-tab-pane>
      </el-tabs>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancel" v-if="(optType == 'view' || form.status != 'PREPARE') && optType != 'save'">返 回</el-button>
        <el-button type="primary" @click="submitForm" v-if="form.status == 'PREPARE' && optType != 'view' && optType != 'save'">保 存</el-button>
        <el-button type="danger" @click="handleSave" v-if="optType == 'save'">确认生产计划</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 分解-->
    <el-dialog :title="title" :visible.sync="fenjieOpen" v-if="fenjieOpen == true" width="960px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" disabled>
        <el-row>
          <el-col :span="12">
            <el-form-item label="计划编号" prop="planCode">
              <el-input v-model="form.planCode" placeholder="请输入计划编号"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计划名称" prop="planName">
              <el-input v-model="form.planName" placeholder="请输入计划名称"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="来源类型" prop="orderSource">
              <el-select v-model="form.orderSource" placeholder="请选择来源类型" clearable>
                <el-option
                  v-for="dict in dict.type.mes_workorder_sourcetype"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="订单编号" prop="sourceCode">
              <el-input v-model="form.sourceCode" placeholder="请输入订单编号" label-width="100px">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="设备编号" prop="deviceCode">
              <el-input v-model="form.deviceCode"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="产品编号" prop="productCode">
              <el-input v-model="form.productCode"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品名称" prop="productName">
              <el-input v-model="form.productName"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="规格型号" prop="productSpc">
              <el-input v-model="form.productSpc" readonly placeholder="请选择产品"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单位" prop="unitOfMeasure">
              <el-input v-model="form.unitOfMeasure" readonly placeholder="请选择产品"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="开工时间" prop="startDate">
              <el-date-picker
                clearable
                v-model="form.startDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择开工时间"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="完工时间" prop="endDate">
              <el-date-picker
                clearable
                v-model="form.endDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择完工时间"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="需求日期" prop="requestDate">
              <el-date-picker
                clearable
                v-model="form.requestDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择需求日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计划数量" prop="quantity">
              <el-input-number :min="1" v-model="form.quantity" placeholder="请输入计划数量"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="客户名称" prop="clientName">
              <el-input readonly v-model="form.clientName" placeholder="请输入客户名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="合并相同产品" prop="mergeSameProduct">
              <el-radio-group v-model="form.mergeSameProduct">
                <el-radio v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.value">{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-tabs type="border-card" v-if="form.planId != null">
        <el-tab-pane label="产品制程">
          <PlanFenjie ref="fenjielist" :planning="form" :optType="optType" @refreshList="getList" @close="fenjieOpen = false"></PlanFenjie>
        </el-tab-pane>
      </el-tabs>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="saveFenJie">保 存</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addPlanning, delPlanning, died, getPlanning, listPlanning, restart, savePlanning, updatePlanning } from '@/api/mes/pro/planning';
import { genCode } from '@/api/system/autocode/rule';
import SourceSelect from '@/components/sourceSelect/proplansourceselect.vue';
import ItemSelect from '@/components/itemSelect/single.vue';
import ProductSelect from '@/components/productSelect/single.vue';
import PlanBom from '@/views/mes/pro/planning/bom/bom.vue';
import PlanFenjie from '@/views/mes/pro/planning/bom/fenjie.vue';

export default {
  name: 'Planning',
  components: {PlanBom, ProductSelect, ItemSelect, SourceSelect, PlanFenjie},
  dicts: ['mes_workorder_sourcetype', 'pro_planning_status', 'mes_item_product', 'mes_workorder_type', 'sys_yes_no'],
  data() {
    return {
      fenjieOpen: false,
      //自动生成编号
      autoGenFlag: false,
      optType: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产计划表格数据
      planningList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        planCode: null,
        planName: null,
        orderSource: null,
        productCode: null,
        productName: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        planCode: [{required: true, message: '计划编号不能为空', trigger: 'blur'}],
        orderSource: [{required: true, message: '来源类型不能为空', trigger: 'blur'}],
        productCode: [{required: true, message: '产品编号不能为空', trigger: 'blur'}],
        productName: [{required: true, message: '产品名称不能为空', trigger: 'blur'}],
        unitOfMeasure: [{required: true, message: '单位不能为空', trigger: 'blur'}],
        quantity: [{required: true, message: '计划数量不能为空', trigger: 'blur'}],
        itemOrProduct: [{required: true, message: '物料类型不能为空', trigger: 'blur'}],
        requestDate: [{required: true, message: '需求日期不能为空', trigger: 'blur'}],
      }
    };
  },
  watch: {
    'form.orderSource'(val) {
      if (val === 'ORDER') {
        this.$set(this.rules, 'sourceCode', [
          {required: true, message: '订单编号不能为空', trigger: 'blur'}
        ])
      } else {
        this.$delete(this.rules, 'sourceCode') // 删除校验规则
      }

      // 重新触发表单校验
      this.$nextTick(() => {
        if (this.$refs.formRef) {
          this.$refs.formRef.clearValidate(['sourceCode']);
        }
      })
    }
  },

  created() {
    this.getList();
  },
  methods: {
    /** 查询生产计划列表 */
    getList() {
      this.loading = true;
      listPlanning(this.queryParams).then(response => {
        this.planningList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.fenjieOpen = false;
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        planId: null,
        planCode: null,
        planName: null,
        orderSource: null,
        sourceCode: null,
        productId: null,
        productCode: null,
        productName: null,
        productSpc: null,
        unitOfMeasure: null,
        quantity: null,
        itemOrProduct: null,
        startDate: null,
        endDate: null,
        requestDate: null,
        status: 'PREPARE',
        remark: null,
        deviceCode: null,
        mergeSameProduct: 'N',
      };
      this.autoGenFlag = false;
      this.resetForm('form');
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm');
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.planId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = '添加生产计划';
      this.optType = 'add';
      this.handleAutoGenChange(true);
    },
    handleSave() {
      let that = this;
      that.$modal.confirm('是否确认完成生产计划编制？确认后该计划将无法更改！').then(function () {
        that.form.status = 'DAIFENJIE';
        savePlanning(that.form).then(response => {
          that.$modal.msgSuccess('修改成功');
          that.getList();
          that.cancel();
        });
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const planId = row.planId || this.ids;
      getPlanning(planId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = '修改生产计划';
        this.optType = 'update';
      });
    },
    handleFenJie(row) {
      this.reset();
      const planId = row.planId || this.ids;
      getPlanning(planId).then(response => {
        this.form = response.data;
        this.fenjieOpen = true;
        this.title = '生产计划分解';
        this.optType = 'fenjie';
      });
    },
    // 查询明
    saveFenJie() {
      this.$modal.confirm('是否执行生产计划？执行后将按照计划类型生成对应的生产工单').then(() => {
        this.form.status = 'YIFENJIE';
        this.$refs['fenjielist'].saveTable();
      });
    },
    // 查询明细按钮操作
    handleInfoView(row) {
      const planId = row.planId || this.ids;
      getPlanning(planId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = '查看生产计划';
        this.optType = 'view';
      });
    },
    // 查询明细按钮操作
    handleInfoSave(row) {
      const planId = row.planId || this.ids;
      getPlanning(planId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = '确认生产计划';
        this.optType = 'save';
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.planId != null) {
            if (this.$refs['bomlist'].bomId && this.$refs['bomlist'].recordId) {
              this.form.bomId = this.$refs['bomlist'].bomId;
              this.form.attr1 = this.$refs['bomlist'].recordId;
            }

            updatePlanning(this.form).then(response => {
              this.$modal.msgSuccess('修改成功');
              this.open = false;
              this.$refs['bomlist'].getList();
              this.getList();
            });
          } else {
            addPlanning(this.form).then(response => {
              this.$modal.msgSuccess('新增成功');
              //this.open = false;
              this.form.planId = response.data;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const planIds = row.planId || this.ids;
      this.$modal
        .confirm('是否确认删除生产计划编号为"' + planIds + '"的数据项？')
        .then(function () {
          return delPlanning(planIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess('删除成功');
        })
        .catch(() => {
        });
    },

    handleConfirm() {
      let that = this;
      this.$modal.confirm('是确认完成工单编制？【确认后将不能更改】').then(function () {
        that.form.status = 'DAIFENJIE';
        that.submitForm();
      });
    },

    //自动生成编号
    handleAutoGenChange(autoGenFlag) {
      if (autoGenFlag) {
        genCode('PLANNING_CODE').then(response => {
          this.form.planCode = response;
        });
      } else {
        this.form.planCode = null;
      }
    },

    handleSelectSource() {
      this.$refs.sourceSelect.showFlag = true;
    },
    handleSelectOrderProduct() {
      this.$refs.productSelect.showFlag = true;
    },
    handleSelectProduct() {
      this.$refs.itemSelect.showFlag = true;
    },
    //销售订单选择弹出框
    onSourceSelected(obj) {
      if (obj != undefined && obj != null) {
        this.form.soId = obj.soId;
        this.form.sourceCode = obj.soNumber;
        this.form.soNumber = obj.soNumber;
        this.form.clientId = obj.clientId;
        this.form.clientCode = obj.clientCode;
        this.form.clientName = obj.clientName;
        this.form.requestDate = obj.deliveryDate;
        this.form.deviceCode = obj.deviceCode;

        this.form.productId = null;
        this.form.productCode = null;
        this.form.productName = null;
        this.form.productSpc = null;
        this.form.unitOfMeasure = null;
        this.form.itemOrProduct = null;
        this.form.quantity = null;
      }
    },
    //产品选择弹出框
    onProductSelected(obj) {
      if (obj != undefined && obj != null) {
        this.form.productId = obj.itemId;
        this.form.productCode = obj.itemCode;
        this.form.productName = obj.itemName;
        this.form.productSpc = obj.specification;
        this.form.unitOfMeasure = obj.unitOfMeasure;
        this.form.itemOrProduct = 'PRODUCT';
        this.form.quantity = obj.quantity;
        if (this.form.planId != null) {
          this.$refs.bomlist.planningbomList = [];
          this.$refs.bomlist.bomCode = undefined;
          this.$refs.bomlist.bomName = undefined;
          this.$refs.bomlist.bomId = undefined;
        }
      }
    },
    //物料选择弹出框
    onItemSelected(obj) {
      if (obj != undefined && obj != null) {
        this.form.productId = obj.itemId;
        this.form.productCode = obj.itemCode;
        this.form.productName = obj.itemName;
        this.form.productSpc = obj.specification;
        this.form.unitOfMeasure = obj.unitOfMeasure;
        this.form.itemOrProduct = obj.itemOrProduct;
        if (this.form.planId != null) {
          this.$refs.bomlist.planningbomList = [];
          this.$refs.bomlist.bomCode = undefined;
          this.$refs.bomlist.bomName = undefined;
          this.$refs.bomlist.bomId = undefined;
        }
      }
    },

    // 中止
    handleDied(row) {
      this.$modal
        .confirm('是否确认中止计划？')
        .then(function () {
          return died({planId: row.planId});
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("操作成功");
        })
        .catch(() => {
        });
    },

    // 中止
    handleRestart(row) {
      this.$modal
        .confirm('是否确认开启计划？')
        .then(function () {
          return restart({planId: row.planId});
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("操作成功");
        })
        .catch(() => {
        });
    },
  }
};
</script>
